﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;
using BEGDC.CC.Web.Control;

namespace BEGDC.CC.Web.Common
{
    /// <summary>
    /// Summary description for AccompanyInput.
    /// </summary>
    public class AccompanyInput : System.Web.UI.Page
    {
        #region Variables
        protected System.Web.UI.WebControls.Button Btn_Save;
        protected System.Web.UI.WebControls.TextBox Ctl_ContactInfo;
        protected System.Web.UI.WebControls.Button Btn_Delete;
        protected BEGDC.CC.Web.Control.UserSearchControl Ctl_AttendeeSearch;
        protected System.Web.UI.WebControls.Button Btn_Close;
        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            string RowIDStr = Request["RowID"];
            string StateStr = Request["State"];

            if (RowIDStr != null)
            {
                this.RowID = Convert.ToInt32(RowIDStr);
            }
            else
            {
                this.RowID = 0;
            }
            this.State = StateStr;

            if (!this.IsPostBack)
            {
                this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
                this.Btn_Delete.Attributes["onclick"] = "javascript:OnDelete();";
                this.Btn_Close.Attributes["onclick"] = "javascript:OnClose();";
                //this.Btn_Save.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_save.gif";
                //this.Btn_Delete.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_delete.gif";
                //this.Btn_Close.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_close.gif";

                if (this.RowID >= 0 && State == "Edit")
                {
                    this.Btn_Delete.Visible = true;
                    InitForm();
                }
            }
        }


        #region RowID State
        protected int RowID
        {
            get { return (int)this.ViewState["AccatelAttendeeRowID"]; }
            set { this.ViewState["AccatelAttendeeRowID"] = value; }
        }
        protected string State
        {
            get { return this.ViewState["AccatelAttendeeState"].ToString(); }
            set { this.ViewState["AccatelAttendeeState"] = value; }
        }
        #endregion

        #region InitForm
        private void InitForm()
        {
            DataTable AttendeeDataTable = null;
            DataRow AttendeeRow = null;
            object SessAcatelAttendee = Session[SessionFlag.AccompanySession];
            if (SessAcatelAttendee != null)
            {
                AttendeeDataTable = (DataTable)SessAcatelAttendee;
            }
            else
            {
                return;
            }

            if (this.RowID >= 0)
            {
                AttendeeRow = AttendeeDataTable.Rows[this.RowID];

                this.Ctl_AttendeeSearch.EmployeeID = (AttendeeRow["EmployeeId"].ToString());
                this.Ctl_AttendeeSearch.EmployeeName = AttendeeRow["Name"].ToString();
                this.Ctl_ContactInfo.Text = AttendeeRow["ContactInfo"].ToString();
            }
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Save.Click += new EventHandler(Btn_Save_Click);
            this.Btn_Delete.Click += new EventHandler(Btn_Delete_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region Update Acatel Attendee Session
        private void UpdateAttendeeSession(out bool isExisted)
        {
            isExisted = false;
            DataTable AttendeeDataTable = null;
            DataRow AttendeeRow = null;
            object SessAcatelAttendee = Session[SessionFlag.AccompanySession];
            if (SessAcatelAttendee != null)
            {
                AttendeeDataTable = (DataTable)SessAcatelAttendee;
            }
            else
            {
                Response.Write("<script>alert('The session is missed, please reopen the application!!')</script>");
                return;
            }

            if (this.State == "New")
            {
                string id = this.Ctl_AttendeeSearch.EmployeeID;
                foreach (DataRow row in AttendeeDataTable.Rows)
                {
                    if (row["EmployeeId"].ToString() == id)
                    {
                        isExisted = true;
                        Response.Write("<script>alert('This person is already in the list!')</script>");
                        return;
                    }
                }

                AttendeeRow = AttendeeDataTable.NewRow();

                AttendeeRow["Id"] = "0";
                AttendeeRow["EmployeeId"] = this.Ctl_AttendeeSearch.EmployeeID.ToString();
                AttendeeRow["Name"] = this.Ctl_AttendeeSearch.EmployeeName;
                AttendeeRow["ContactInfo"] = this.Ctl_ContactInfo.Text;

                AttendeeDataTable.Rows.Add(AttendeeRow);
            }
            else if (this.State == "Edit" && this.RowID >= 0)
            {
                AttendeeRow = AttendeeDataTable.Rows[this.RowID];
                AttendeeRow["EmployeeId"] = this.Ctl_AttendeeSearch.EmployeeID.ToString();
                AttendeeRow["Name"] = this.Ctl_AttendeeSearch.EmployeeName;
                AttendeeRow["ContactInfo"] = this.Ctl_ContactInfo.Text;
            }
            else if (this.State == "Delete" && this.RowID >= 0)
            {
                AttendeeDataTable.Rows.RemoveAt(this.RowID);
            }
            else
            {
                return;
            }

            Session[SessionFlag.AccompanySession] = AttendeeDataTable;
        }
        #endregion

        #region Save
        void Btn_Save_Click(object sender, EventArgs e)
        {
            if (this.Ctl_AttendeeSearch.EmployeeID == string.Empty || this.Ctl_AttendeeSearch.EmployeeName == "")
            {
                Page.RegisterClientScriptBlock("", "<script>alert('Please select attendee name. 请选择陪同人员!');</script>");
                //WebUtility.ShowAlert(this.Page, "Please select attendee name.\\n 请选择陪同人员！");
                return;
            }

            try
            {
                bool isExisted = false;
                UpdateAttendeeSession(out isExisted);
                if (!isExisted)
                {
                    Response.Write("<script>window.close();window.returnValue='reload';</script>");
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            //Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }
        #endregion

        #region Delete
        void Btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                this.State = "Delete";
                //UpdateAttendeeSession();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }
        #endregion
    }
}
